Mencipta dan menyunting templat Bantuan:Templat

Templates are created and edited in much the same way as any other page on WikiMedia: choose an appropriate name, navigate to that page, then click the Edit tab or create a new page as needed. The only real difference is that templates will normally be placed in the "Template:" namespace rather than the (unprefixed) article namespace. Anything that can be included on a normal page or article can be included on a template, including other templates (though as a practical matter templates will often have relatively small contents). Beyond that, templates have access to programming features - parameters, parser functions, and magic words - which allow for context dependent use,[2] and special tags that help control which information is transcluded and which is not.

When creating templates, it's best to follow a few simple principles:

  • Choose a short, descriptive name. Some people suggest that template names be completely small caps, some that spaces in template names should be avoided, but there is currently no hard guideline.
  • Make a quick search of template space to make sure that the template you're creating hasn't already been done. It is sometimes easier and better to enhance an already existent template than to create a new template from scratch.
  • Make sure your template is properly documented and included in the correct categories. Categorizing your template and documenting it will make it easier for other editors to find and use.
    • To add documentation, put the {{Documentation}} template inside 'noinclude' tags on the template page. This will create a documentation subpage that you can edit
    • Categories that apply to the template itself should be added to the template page (inside 'noinclude' tags, with the {{Documentation}} template). Categories that are to be transcluded to the transclusion page should be placed on the template page inside 'includeonly' tags.
  • Try to make your template modular: i.e. capable of standing on its own and in a variety of different page contexts, without breaking, producing poor output, or messing up a page's layout. In general, you want your template to be transparently useful to other editors - the more effort they have to put into understanding how your template is used, the less they will be willing to use it.

Fungsi Parser

Parser functions are the WikiMedia toolkit for simple branchings, calculations, text manipulations, and the like. As a group, they take one or more parameters and return a wikitext value based on the parameters. There are two basic forms for Parser functions (distinguished by whether a pound sign follows the opening brackets):

  • Core parser functions, of the form {{functionName: parameter | parameter | ... }}. These functions are part of the core wikimedia software, and should be available in all projects.
  • Parser functions from the parserFunctions extension, of the form {{#functionName: parameter | parameter | ... }}. These functions are part of the parserFunctions extension, and may not be available everywhere.

Parser functions are primarily used for the evaluation of parameters or magic words, in order to produce different results in different contexts.

Core parser functions generally deal with text manipulation and WikiMedia specific tasks.

Examples of core parser functions
DescriptionText EnteredResult
Uppercasing text{{uc: Heavens to BETSY! }}HEAVENS TO BETSY!
Lowercasing text{{lc: Heavens to BETSY! }}heavens to betsy!
Getting a namespace name{{NS: 1 }}Perbincangan
Getting a wikipedia URL{{fullurl: pagename }}//ms.wikipedia.org/wiki/Pagename

The parserFunctions extension gives more more programming-oriented parser functions.

Examples of extension parser functions
DescriptionText EnteredResult
Testing between options{{#ifeq: yes | yes | Hooray...! | Darn...! }}
{{#ifeq: yes | no | Hooray...! | Darn...! }}
Hooray...!
Darn...!
Testing if a parameter is set{{#if: {{{param|}}} | Hooray...! | Darn...! }}Darn...!
making a calculation{{#expr: ( 27 * 46 / pi ) round 2 }}395.34

These functions are discussed in greater detail on their respective help pages.

Parameter-Parameter

Parameters are special codes that allow wikitext to be fed to a template; the template can be constructed to produce different outcomes depending on the values of the parameters it receives. Parameters may have names, or may be referenced by the position of values provided in the template tag (the order of the parameters in the template itself is irrelevant). Parameters in a template take the form {{{...}}}, where the tripled curled-braces surround the name of a named parameter or the number of a positional parameter. Values entered for parameters can be quite long (see Help:Long parameter demo), and if needed the transclusion of an entire page may be used as the value for a parameter.

To see how parameters work, consider this example. Assume there is a template called 'peoplepets' that produces some text about people and their pets. A template tag for this template might look like the following:

  1. {{ peoplepets | John | Mary | age = 6 | Fido | small | kind = dog }}
  2. {{ peoplepets | Bill | Susan | age = 7 | Queenie | fat | kind = cat }}

The contents of the template itself, on the page Template:Peoplepets, looks as follows:

{{{1}}} and {{{2}}} own a {{{4}}} [[{{{kind}}}]] named {{{3}}} who is {{{age}}} years old.

Thus, the two template tags above will transclude the following text, respectively:

  1. John dan Mary mempunyai seekor small dog bernama Fido yang berumur 6 tahun.
  1. Bill dan Susan mempunyai seekor fat cat bernama Queenie yang berumur 7 tahun.

Things to note:

  • The numbering of positional parameters skips over named parameters (as with 'age' above)
  • The leading and trailing spaces around the values of named parameters in the template tags are stripped by the template.
    • This is not true for unnamed parameters, which preserve trailing and leading whitespace. However, browsers render multiple whitespace characters as a single space, so the extra spaces around 'Fido', 'fat' and the other unnamed parameter values aren't immediately visible. This can be confusing if unnamed parameters are used for something other than display, such as links.
  • The order in which parameter values are used in the template doesn't matter.
  • The number of a positional parameter is treated as though it were a name: {{ peoplepets | John | ... }} could have been written {{ peoplepets | 1 = John | ... }}. This is useful when positional parameters need to be entered out of order.
  • Values may be empty. Here the second positional parameter and the 'age' parameter have empty values: Template tag:{{ peoplepets | John || age = | Fido | small | kind = dog }}Produces (note the missing words): John dan mempunyai seekor small dog bernama Fido yang berumur tahun.
  • Values can remain unprovided. Here the 'kind' parameter has been omitted:Template tag: {{ peoplepets | John | Mary | age = 6 | Fido | small }}Produces (this displays the missing variable): John dan Mary mempunyai seekor small [[{{{kind}}}]] bernama Fido yang berumur 6 tahun.

Parameter default

Default values can be specified for parameters using the vertical pipe character: |. The above example template could be rewritten as follows (in which 'friend' is the default value for positional parameter 2, the 4th positional parameter defaults to emptiness, and the 'kind' of the pet defaults to 'dog'):

{{{1}}} and {{{2|friend}}} own a {{{4|}}} [[{{{kind|dog}}}]] named {{{3}}} who is {{{age}}} years old.

A template tag like the following: {{ peoplepets | Bill || age = 7 | Queenie }} will then produce the phrase Bill and friend own a dog named Queenie who is 7 years old.

Latihan parameter dan susun atur tag

Parameters that are not used in the template are ignored if they are provided by the template tag. This was intended to prevent transclusions from breaking if a template is changed in a way that removes a parameter; there's no need to go to every page a template is transcluded onto and change the tags. This has an added benefit, however, of making some template tags more readable, through spacing, or by adding comments. Using {{t3d}}:

Dummy parameters
DescriptionText EnteredResult
Adding comments using template {{t3d}}
{{t3d |a|b|c| row 1      |d|e|f| row 2      |g|h|i| row 3}}
a b c

d e f
g h i

Since {{t3d}} does not use parameters {{{4}}}, {{{8}}}, or {{{12}}} the 'row #' values have no effect on the produced wikitext.

Dummy named parameters can be used in any template tag; simply choose an unused parameter name and add it into the tag as 'unusedname = value'. One special case of a named dummy parameter is to use an empty string as the parameter name. This acts like a named parameter in that it isn't counted when assessing the unnamed parameter positions, but it doesn't risk accidentally conflicting with an actual named parameter.

Named dummy parameter (empty string)
DescriptionText EnteredResult
dummy variables with {{chess position}}
{{chess position|= comments go here 8 |rd|nd|bd|qd|kd|bd|nd|rd|= 7 |pd|pd|pd|  |  |pd|pd|pd|= 6 |  |  |  |  |  |  |  |  |= 5 |  |  |  |pd|pd|  |  |  |= standard opening 4 |  |pl|  |  |  |  |pl|  |= 3 |  |  |  |  |  |  |  |bl|= odd move 2 |pl|  |pl|pl|pl|pl|  |pl|= 1 |rl|nl|bl|ql|kl|  |nl|rl|=    a  b  c  d  e  f  g  h|30}}
8
7
6
5
4
3
2
1
abcdefgh

{{chess position}} uses the first 64 unnamed parameters to specify pieces; adding in unnumbered dummy parameters for comments would have required that the template be rewritten (or that the unnumbered parameters be specified explicitly using the '#=value' format).

Kekangan terhadap parameters dan nilai parameter

  • Unnamed parameters cannot be assigned a value containing an equals sign (=); the parser will treat the equals sign as the assignment of a named parameter. There are several indirect means around this limitation:
    • In the template, write the value as the default of an undefined parameter:
      • {{{...| a=b }}}, where the ellipsis can be number of an unused positional parameter, or can be a blank string (a null parameter).
    • In the article, assign the values to the template parameters with one of the following methods:
      • {{templatename| 1= a=b }}, where the '1=' notation explicitly names the first unnamed parameter.
      • Use the special template {{=}}, e.g. {{template name| a{{=}}b }}
      • Use the HTML entity for an equals sign: =. This is only useful for text representation; If the equals sign needs to be processed as an actual equals sign (as in another template) this approach won't work.
  • Parameter values cannot contain vertical pipe (|) characters; the parser will treat the pipe character as a separator rather than as text. Pipes are possible within wikilinks (country=[[United States|USA]]) or nested templates (e.g. volume= {{convert|30|acre.ft|m3}}). Indirect means around this limitation are as follows:
    • Use the special template {{!}}, which returns a valid pipe character. Use this method if you need something that will be read as an actual pipe character (for instance, if you're using the parameter to modify the structure of a wikitext table)
    • Use the HTML entity for a vertical pipe: |. This is only useful for text representation; If the pipe needs to be processed as an actual pipe this approach won't work.
  • Parameter values cannot contain unmatched sequential curled brackets - }} or {{. The parser will try to parse them as the beginning or ending of a template tag or parameter, and will throw errors somewhere. Parameters can, however, contain other parameters, magic words, or template tags.
    • again, this can be circumvented using 'nowiki' tags or HTML entities.

Parameters do not get expanded when they are inside nowiki tags or XML-style extension tags. Thus, the following will not work within a template - <myextension xparam={{{tparam}}}> ... </myextension> - because the parameter is not expanded:

Because template tags, parser functions, magic words, and parameters are all defined using curled braces, there are times when their combined use can be ambiguous. For example, the construction {{{{{NAMESPACE}}}}} might be interpreted as:

  1. {{ {{{NAMESPACE}}} }} (a template tag whose name is specified by the named parameter NAMESPACE)
  2. {{{ {{NAMESPACE}} }}} (a parameter whose name is specified by the value of the magic word NAMESPACE).

In general, wherever the parser finds more than 3 opening braces in a row, it will assume it is a parameter if the matching closing braces are 3-in-a-row, or as a template tag if only two of the matching closing braces are contiguous. In some cases it is advisable to add a space in the opening brackets to clarify the construction. For instance, the {{{{{NAMESPACE}}}}} construction from above will always default to the first interpretation; it might be better to use {{{ {{NAMESPACE}}}}} or {{ {{{NAMESPACE}}}}} as required so that the intention of the code is clear to other editors.

Nesting templates

Templates may contain other templates - usually called 'nesting'. As the template is processed, the wikitext produced by any nested templates is transcluded into the nesting template, so that the final product is essentially processed from the most deeply nested template out. While fairly straight-forward in application, it involves some noteworthy quirks and tricks.

To pass a parameter value to a nested template, place a parameter tag as the value of one of the nested template's parameters.

  • Examples:
    • Template:A contains "the quick brown {{B|{{{3}}}}} jumped over...". This takes the value passed to the third positional parameter of Template:A and passes it as the first positional parameter of Template:B, then returns the wikitext produced by B as part of the phrase.
    • Template:A contains "the quick brown {{B|waldo={{{3}}}}} jumped over...". As above, except the third positional parameter of Template:A is passed to the named parameter 'waldo' of Template:B.

Template parameters themselves can be chosen conditionally.

  • Example:
    • Template:A contains "the quick brown {{B|{{{3}}}=fox}} jumped over...". Template:A passes the word 'fox' to a parameter of Template:B that's named in A's third positional parameter.

The Wikimedia software will allow a template to call itself, but will stop after one iteration to prevent an infinite loop.

When a nested template contains unmatched curled-brackets - as in {{lb}} - the unmatched brackets are treated as text during processing, and do not affect the parsing of brackets in the nesting template. If the nested template is substituted, however, the substitution is processed first, and this will change how braces are parsed in the nesting template. This has little practical use, but can occasionally introduce unexpected errors.

See the help:advanced templates and meta:Help:Recursive conversion of wikitext for more information.

Noinclude, includeonly, and onlyinclude

Several tags are available to control what does and doesn't get transcluded from a template.[3] The three tags are noinclude, includeonly, and onlyinclude. They are used in the same fashion as standard HTML or XML blocks: opening and closing tags in angled brackets (e.g. <noinclude>affected wikitext</noinclude>).

Perhaps the most common issue with the use of these blocks is unwanted spaces or lines. It is important to remember that the effect of these tags ends immediately after the last angle bracket, not on the next line or with the next visible character.

These tags can be nested inside each other, though (for a given page) this really only applies to the 'onlyinclude' tag; nesting 'includeonly' and 'noinclude' tags is fairly pointless. Be careful not to split the tags, however. Constructions like this <onlyinclude>abc<includeonly>def</onlyinclude>ghi</includeonly> will not work as expected. Use the 'first opened, last closed' rule that is standard for XML.

Note further that expansions may not occur the way expected. For instance, ~<includeonly>~</includeonly>~~ will be displayed as ~~~ in the template, and will be transcluded as ~~~~, but it will not be expanded in either case (as user name or the name/date combination). The template would have to be substituted to get this wikitext to expand.

noinclude

The 'noinclude' tag is used to prevent text on the template page from being transcluded onto other pages. This is often used for:

  • documentation
  • categories that apply to the template itself
  • interwiki links to related templates on other wikis.

Its use is straightforward:

If this text were in a template page, this part would be transcluded<noinclude>but this text would not be transcluded</noinclude>

One of the peculiarities of 'noinclude' tags is that the text contained in them is actually processed, even though it is not included in the rendered output. This has to do with error reporting: the processor needs to retain the text while it checks that there is a valid </noinclude> closing tag, and this consumes system resources. For templates that are not heavily used, this is rarely a problem, but on some templates excess text (and interwiki links in particular) can create a tremendous overhead for the system. As a rule, interwikis and documentation should be placed on a /doc subpage (such as is generated automatically by the {{Documentation}} template).

includeonly

The 'includeonly' tag is the opposite of the 'noinclude' tag. Text within an include only block is only included when the page is transcluded, is does not appear on the template page itself. This is frequently used for:

  • categories that apply to the transclusion page
  • hiding messy text or error messages that occur on the template page itself (often because parameters that need a value are undefined)

Its use is straightforward:

If this text were in a template page, this part would appear on the template page and on the transclusion page<includeonly>but this text would only appear on the transclusion page</includeonly>

onlyinclude

The 'onlyinclude' tag literally only includes the material that is between the tags; anything else on the page - even text within 'includeonly' tags, will appear on the template page but will not be included. This is not frequently used, but might be useful when only a small bit of text in the middle of an otherwise large page is to be transcluded.It is used like this:

If this text were in a template page, this part would be visible there, but would not be transcluded<includeonly>This text would not be visible on the template page, nor would it be transcluded</includeonly><onlyinclude>this text is all that would be transcluded</onlyinclude>

Magic words

Rencana utama: mw:Help:Magic words

There are three general types of magic words on Wikimedia pages. One - parser functions - are already discussed above. The second type - behavioral switches - involves page structure modifications which are of little use on templates. The last type, however - variables (or system variables) - are often used on templates. These magic words provide information directly from the system itself: local dates and times, information about the current articles or pages, and even information about the wiki itself.

System variables take the format {{...}} where the enclosed text is always entirely capitalized. A small number of system variables take a parameter as well, using the standard colon separator - {{MAGICWORD:parameter}}. If the enclosed text starts and ends with doubled underscores (__...__) it is a behavioral switch, not a variable.

Examples of System Variables
DescriptionText EnteredResult (for this help page)
Page names{{PAGENAME}}
{{FULLPAGENAME}}
Templat
Bantuan:Templat
Name of the current namespace{{NAMESPACE}}Bantuan
Number of registered users{{NUMBEROFUSERS}}248,989
Number of pages in a given category{{PAGESINCATEGORY:"Weird Al" Yankovic albums}}
Current software version{{CURRENTVERSION}}1.35.0-wmf.15 (5baae39)
Timestamp of last revision{{REVISIONTIMESTAMP}}20190301111046

The PAGENAME and NAMESPACE variables are particularly useful, and frequently used, to change template behavior based on context. For example, if the template transcludes a category link (e.g. cleanup templates, which transclude a link categorizing the page as a page which needs cleanup), it will often check the NAMESPACE variable to make sure that talk pages, user pages, or anywhere else the tag might incidentally be placed do not themselves get categorized as pages needing cleanup.

Other editing information

  • If the first included character of a template is one of the Wiki markup characters :;*#, then it is processed as though it is at the beginning of a line (even when the template tag is not). This allows the creation of various kinds of lists in templates where the template may not always be in the correct place for a list. To avoid this effect use <nowiki>#</nowiki> or a numeric character reference (Templat:Mlw) or HTML entity such as &#58; for a colon. This NCR is also useful in conjunction with definition lists.
  • When a page called for transclusion is a redirect page, the redirect target is included instead.
  • A page that consists of nothing but the transclusion of another page is similar to a redirect, with minor differences. This method can be used to create functional "double redirects", should a need for such arise.
  • For debugging templates the following techniques are sometimes helpful
    • using 'subst:' - substituting a template (rather than transcluding it) can show more clearly what is happening when the template is transcluded.
    • using 'msgnw:' - this keyword (short for "message, nowiki") transcludes the wikitext of the template page, more or less, rather than the processed contents. it is not perfect: lists are rendered, comments are removed, and single newlines are replaced with spaces (which is particularly confounding when transcluding wikitext tables).
Transclusion, substitution and message no wiki of {{msgnw demo}}
Transclusion
{{msgnw demo}}
Substitution
{{subst:msgnw demo}}
Message, no wiki
{{msgnw:msgnw demo}}
Templat:Msgnw demo
  1. a
  2. b

jklmno

[[:Templat:Msgnw demo]]

Click the edit link for this section to see how the substituted section is different.